package com.heima.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.util.AppJwtUtil;
import com.heima.media.Dto.LoginDto;
import com.heima.media.entity.WmUser;
import com.heima.media.mapper.WmUserMapper;
import com.heima.media.service.IWmUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 自媒体用户信息表 服务实现类
 * </p>
 *
 * @author lyd
 * @since 2022-06-20
 */
@Service
public class WmUserServiceImpl extends ServiceImpl<WmUserMapper, WmUser> implements IWmUserService {
    /**
     * 自媒体端新增用户
     */
    @Override
    public ResponseResult<WmUser> saveWmUser(WmUser wmUser) {
        //关键数据校验
        if (wmUser == null || wmUser.getApUserId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        /*
            新增自媒体用户
         */
        //判断当前用户是否存在 存在则直接返回用户信息
        LambdaQueryWrapper<WmUser> query = new LambdaQueryWrapper<>();
        query.eq(WmUser::getApUserId,wmUser.getApUserId());
        WmUser one = this.getOne(query);
        if (one != null){
            return ResponseResult.okResult(one);
        }
        //不存在则新建自媒体用户
        wmUser.setCreatedTime(new Date());
        this.save(wmUser);

        return ResponseResult.okResult(wmUser);

    }

    @Override
    public ResponseResult login(LoginDto dto) {
        //关键数据校验
        if (dto ==null || StringUtils.isEmpty(dto.getName()) || StringUtils.isEmpty(dto.getPassword())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //根据用户名查询用户
        LambdaQueryWrapper<WmUser> query = new LambdaQueryWrapper<>();
        query.eq(WmUser::getName,dto.getName());
        WmUser one = this.getOne(query);

        //不存在提示用户不存在
        if (one == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //存在 取出用户的salt
        String salt = one.getSalt();
        //获取用户输入的密码
        String pwd = dto.getPassword();
        //拼接两部分
        String password = pwd + salt;

        //采用md5加密
        String md5 = DigestUtils.md5DigestAsHex(password.getBytes());

        //对比数据库中存储的密码
        //如果不一致 提示密码错误
        if (!md5.equals(one.getPassword())){
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
        }
        //一致  使用jwt生成token
        Map<String,Object> userinfo = new HashMap<>();
        userinfo.put("userId",one.getId());
        String token = AppJwtUtil.getToken(userinfo);

        //返回给前端
        WmUser adUser = new WmUser();
        adUser.setName(one.getName());
        Map<String,Object> map = new HashMap<>();
        map.put("user",adUser);
        map.put("token",token);
        return ResponseResult.okResult(map);
    }
}
